local guye = fk.CreateSkill{
  name = "zy__guye",
}
local U = require "packages/utility/utility"
local zy__guye_ = {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if not player.dead and player:hasSkill(self) and table.every(player.room.alive_players, function(p) return not p.dying end) then
      return target == player
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(3, "top")
    local handcards = player:getCardIds(Player.Hand)
    local cardmap = room:askForArrangeCards(player, self.name,
    {cards, handcards, "Top", "$Hand"}, "#zy__guye-invoke")
    U.swapCardsWithPile(player, cardmap[1], cardmap[2], self.name, "Top")
    local ignore = 0
    for i = 1, #cards do
      if not cardmap then break end
      if cards[i] ~= cardmap[1][i] then
        ignore = 1
      end 
    end 
    if ignore == 0 and player:getMark("zy__guye_zeroselect-turn") == 0 then
      local viewCards = room:getNCards(3)
      local card2 = room:askForCard(player, 1, 1, true, self.name, true, ".|.|.|^(hand,equip)", "#zy__guye-view", viewCards)
      if #card2 > 0 then
        table.removeOne(viewCards, card2[1])
        room:obtainCard(player, card2[1], false)
        player.room:setPlayerMark(player, "zy__guye_zeroselect-turn", 1)
      end
      for i = #viewCards, 1, -1 do
        table.insert(room.draw_pile, 1, viewCards[i])
      end
    end
  end,
}
guye:addEffect(fk.Damage, zy__guye_)
guye:addEffect(fk.Damaged, zy__guye_)
return guye